package com.yoki.loginview;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.yoki.loginview.db.AccountDBHelper;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText UserTB;
    private EditText PwdTB;
    private EditText PwdTB2;
    private EditText PhoneTB;
    private EditText ntTB;
    private Button RegBtn;
    private Spinner spinner;
    private String sex;
    private int sexNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        UserTB = findViewById(R.id.CUserTB);
        PwdTB = findViewById(R.id.CPwdTB);
        PwdTB2 = findViewById(R.id.PwdTB2);
        PhoneTB = findViewById(R.id.PhoneTB);
        RegBtn = findViewById(R.id.RegBtn);
        ntTB = findViewById(R.id.ntTB);
        RegBtn.setOnClickListener(this);
        spinner = findViewById(R.id.sexSpinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int curyid = i;
                TextView tv = (TextView)view;
                if(curyid==0)
                {
                    tv.setTextColor(getResources().getColor(R.color.colorPrimaryDark)); //设置颜色
                }
                else
                {
                    tv.setTextColor(getResources().getColor(R.color.VeryBlack)); //设置颜色

                }
                sex=spinner.getSelectedItem().toString();
                if(sex.equals("男"))
                {
                    sexNum=1;
                }
                else if(sex.equals("女"))
                {
                    sexNum=2;
                }
                tv.setTextSize(18.0f); //设置大小
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        String UserName = UserTB.getText().toString();
        String Password = PwdTB.getText().toString();
        String Password2 = PwdTB2.getText().toString();
        String Phone = PhoneTB.getText().toString();
        String nicheng = ntTB.getText().toString();
        AccountDBHelper helper = new AccountDBHelper(RegisterActivity.this,AccountDBHelper.VERSION_1);
        Cursor cursor = helper.select(null,null);
        int isReg = 0;
        cursor.moveToFirst();
        for(int i=0;i<cursor.getCount();i++)
        {
            Log.d("db",cursor.getString(1));
            if(UserName.equals(cursor.getString(1)))
            {
                isReg++;
                break;
            }
            cursor.moveToNext();
        }
        if(UserName.equals(""))
        {
            UserTB.requestFocus();
            Toast.makeText(this,"用户名为空，请输入",Toast.LENGTH_SHORT).show();
        }
        else if(Password.equals(""))
        {
            PwdTB.requestFocus();
            Toast.makeText(this,"密码为空，请输入",Toast.LENGTH_SHORT).show();
        }
        else if(!Password.equals(Password2))
        {
            PwdTB2.requestFocus();
            Toast.makeText(this,"两次输入的密码不相同，请重新输入",Toast.LENGTH_SHORT).show();
        }
        else if(sex.equals("请选择性别"))
        {
            Toast.makeText(this,"未选择性别，请选择",Toast.LENGTH_SHORT).show();
        }
        else if(nicheng.equals(""))
        {
            ntTB.requestFocus();
            Toast.makeText(this,"昵称为空，请输入",Toast.LENGTH_SHORT).show();
        }
        else if(Phone.equals(""))
        {
            PhoneTB.requestFocus();
            Toast.makeText(this,"手机号为空，请输入",Toast.LENGTH_SHORT).show();
        }
        else if(isReg==0)
        {
            long Long = helper.insert(UserName,Password,Phone,sexNum,nicheng);
            Toast.makeText(this,"注册成功！",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
            intent.putExtra("NameText",UserName);
            intent.putExtra("password",Password);
            startActivity(intent);
            finish();

        }
        else if(isReg!=0)
        {
            Toast.makeText(this,"账号已注册！",Toast.LENGTH_SHORT).show();
        }
    }
}
